package cn.texous.demo.demojvm.stack;

/**
 * insert description here.
 *
 * @author Showa.L
 * @since 2019/11/11 15:47
 */
public class GCDemo {

    public static GCDemo gcDemo = null;

    public static void main(String[] args) throws InterruptedException {
        gcDemo = new GCDemo();
        printLog(gcDemo);
        gcDemo = null;
        System.gc();
        Thread.sleep(5000);
        printLog(gcDemo);

        gcDemo = null;
        System.gc();
        Thread.sleep(5000);
        printLog(gcDemo);

        gcDemo = null;
        System.gc();
        printLog(gcDemo);
    }

    private static final void printLog(GCDemo gcDemo) {
        if (gcDemo == null) printAlive(false); else printAlive(true);
    }

    private static final void printAlive(boolean alive) {
        if (alive) {
            System.out.println("I'm still alive");
        } else {
            System.out.println("I'm dead");
        }
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("finalize run");
        gcDemo = this;
    }
}
